<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>对象枚举</title>
  </head>
  <body>
    <script>
      /*
        实现链式调用
        */
      var person1 = {
        name: "黄越",
        smoke: function () {
          console.log("smoke,接下来是drink" + this["d" + "rink"]);
          return this;
        },
        drink: function () {
          console.log("drink" + JSON.stringify(this));
          return this;
        },
      };
      person1.smoke().drink();

      /*
      对象的枚举(遍历)
      1.for in
      2.hasOwnProperty
      3.in
      4.instanceof
      */
     Object.prototype.str = '这是原型上的'
      var obj = {
          name:'黄越',
          age:18,
          sex:'male',
          height:180,
          weight:120,
      }
       //1.for in 
       console.log("-------------------------------")
       console.log("for in 遍历对象")
       for (const key in obj) {
           console.log(key + ":" + obj[key])
       }
       //2.hasOwnProperty() 判断属性是否是自己的，是返回true 否则返回false
       console.log("-------------------------------")
       console.log("hasOwnProperty:是否是自己的属性")
       for (const key in obj) {
           if (obj.hasOwnProperty(key)) {
               console.log("不在原型上的属性："+ obj[key])  
           }else{
               console.log(obj[key])
           }
       }
       //3.in 只能用来判断该属性是否是改对象的
       console.log("-------------------------------")
       console.log("in:是否是该对象的属性")
       console.log('name是obj的属性吗：'+('name' in obj));
       console.log('str是obj的属性吗：'+('str' in obj));
       console.log('lastname是obj的属性吗：'+ ('lastname' in obj))

       //4.instanceof
       //A instanceof B
       //看A的原型链上是否有B的原型
       console.log("-------------------------------")
       console.log("instanceof:看A的原型链上是否有B的原型")
       function Person (){}
       var person = new Person;

       console.log('person上有Person的原型吗：'+(person instanceof Person));
       console.log('person上有Object的原型吗：'+(person instanceof Object));
       console.log('person上有Arry的原型吗：'+(person instanceof Array))

    </script>
  </body>
</html>
